﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-11>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：打印通知单
//********************************************************************************//
layui.use(['form', 'jquery', 'laydate', 'element'], function () {
    var form = layui.form()
    , layer = layui.layer
    , $ = layui.jquery
    , laydate = layui.laydate
    , element = layui.element();
    /*initial load start*/
    initcontrol();
   
    Search();
    loadReport();
    
    
    /*initial load end*/

    /*initial*/
    function initcontrol() {
        //加载部门
        //$("#cBM option").remove();
        var list = getYSBMList("");
        //$("#cBM").append("<option value='0'>  </option>");
        for (var i = 0; i < list.length; i++) {
            $("#cBM").append("<option value='" + list[i]["Value"] + "'>" + list[i]["Text"] + "</option>");
        }
        $("#cBM").get(0).selectedIndex = 0;
        form.render("select");
    }
    /*end initial*/

    /*event*/

    form.on("select(cAssignObject)", function (data) {
        if (data.value == 1) {
            //角色
            $("#ContentList1").hide(700);
            $("#ContentList2").show(1000);

            $("#lab_Name").hide(1000);
            $("#cName").hide(1000);
            $("#labBM").hide(1000);
            $("#divCBM").hide(1000);
            loadrole();
            $("#lsOperateInfo").attr("vid", 0);
        } else if (data.value == 0) {
            //用户

            $("#lab_Name").show(1000);
            $("#cName").show(1000);
            $("#labBM").show(1000);
            $("#divCBM").show(1000);
            $("#ContentList2").hide(700);
            $("#ContentList1").show(1000);
            load();
            $("#lsOperateInfo1").attr("vid", 0);
        }
        
        
    });


    $("#oSearch").click(function () {

        Search();
    });
    function Search()
    {
        var index = layer.load(1);
        if ($("#cAssignObject").val() == 0) {
            load();
        } else if($("#cAssignObject").val()==1){
            loadrole();
        }
        layer.close(index);
       // SelectFirst();
        
    }
    $("#iBtnSave").click(function () {
        SaveReportRole();       
    });

    

    

    /*event end*/

    /*list*/
    function createItemStr(obj) {
        var str = $("#ItemStr1 tr").prop("outerHTML");
        var itemstr, userid, username, account,department;
        userid = obj["USERID"];
        username = obj["USERNAME"];
        account = obj["ACCOUNT"];
        department = obj["DEPARTMENTNAME"];
        itemstr = str.replace(new RegExp("objUserId", "gm"), userid)
                        .replace(new RegExp("objUserName", "gm"), username)
                        .replace(new RegExp("objAccount", "gm"), account)
                        .replace(new RegExp("objDepartmentName", "gm"), department)
        ;
        return itemstr;
    }

    function createItemStrRole(obj) {
        var str = $("#ItemStr2 tr").prop("outerHTML");
        var itemstr, RoleID, RoleName;
        RoleID = obj["ROLEID"];
        RoleName = obj["ROLENAME"];

        itemstr = str.replace(new RegExp("RoleID", "gm"), RoleID)
                        .replace(new RegExp("RoleName", "gm"), RoleName)

        ;
        return itemstr;
    }

    function SelectFirst()
    {
        if ($("#cAssignObject").val() == 0) {
            var userid = $("#ContentList1 .datarow:first").attr("vid");
            $($("#lsOperateInfo").attr("vid", userid));
            $("#ContentList1 .datarow[vid='" + userid + "']").css("background-color", "lightgray");
        } else if ($("#cAssignObject").val() == 1) {
            var roleid = $("#ContentList2 .datarow:first").attr("vid");
            
            $($("#lsOperateInfo1").attr("vid", roleid));
            $("#ContentList2 .datarow[vid='" + roleid + "']").css("background-color", "lightgray");
        }
    }

    function bindListOperate()
    {
        $("#ContentList1 .datarow").unbind("click").click(function () {
            //清除之前选择项变色
            $("#ContentList1 .datarow[vid='" + $("#lsOperateInfo").attr("vid") + "']").css("background-color", "");
            //记录当前选择项
            var userid = $(this).attr("vid");
            $($("#lsOperateInfo").attr("vid", userid));
            //将当前选择项变色
            $("#ContentList1 .datarow[vid='" + userid + "']").css("background-color", "lightgray");
            loadReportRole();
            $(".datarow1").each(function () {
                $(this).css("background-color", "");
            });
        });

        $("#ContentList2 .datarow").unbind("click").click(function () {
            $("#ContentList2 .datarow[vid='" + $("#lsOperateInfo1").attr("vid") + "']").css("background-color", "");
            var role = $(this).attr("vid");
            $($("#lsOperateInfo1").attr("vid", role));
            //将当前选择项变色
            $("#ContentList2 .datarow[vid='" + role + "']").css("background-color", "lightgray");
            loadReportRole();
            $(".datarow1").each(function () {
                $(this).css("background-color", "");
            });
        });
    }

    /**
   
    **/
    function loadrole()
    {         
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/XT/XT.ashx",
            data: {
                "op":"role"
            },
            dataType: "json",
            success: function (rs) {
                var strs = "";
                for (var i in rs) {
                    strs = strs + createItemStrRole(rs[i]);
                }
                $("#ContentList2").find(".datarow").remove();
                $("#ContentList2").append(strs);
                $("#lsCount").text(rs.length);
                $("#sCont").text("角色数:");
                bindListOperate();
            }
        });
    }

    function load() {
        var name = $("#cName").val();
        var bm = $("#cBM").val();
        var active = 1;
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/XT/XT.ashx",
            data: {
                "op": "yhxx",
                "name": name,
                "bm": bm=="0"?"-1":bm,
                "active": 1
            },
            dataType: "json",
            success: function (rs) {
                
                var strs = "";
                for (var i in rs) {
                    strs = strs + createItemStr(rs[i]);
                }
                $("#ContentList1").find(".datarow").remove();
                $("#ContentList1").append(strs);
                $("#lsCount").text(rs.length);
                $("#sCont").text("人员数:");
                bindListOperate();
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        
    }

   
    form.on('checkbox(Title)', function (data) {

        var s = data.elem;
        var vid = $(s).attr("vid");
        $("table[vid='" + vid + "']").find(":checkbox").prop("checked", data.elem.checked);
        
        form.render("checkbox");
    });

    form.on('checkbox(SelectAll)', function (data) {
        
        $(":checkbox").prop("checked", data.elem.checked);
        form.render("checkbox");
    });

    form.on('checkbox(Detailed)', function (data) {
        var s = data.elem;
        var parentID = $(s).attr("parentid");
      
        $("input[vid='" + parentID + "']").prop("checked", '')

        $("table[vid='" + parentID + "'] :checkbox").each(function () {
            if ($(this).prop("checked") == true) {
                
                $("input[vid='" + parentID + "']").prop("checked",true);
            }
        });
        form.render("checkbox");
    });

    function loadReportRole() {
        $(":checkbox").prop("checked",'');
        var _RoleID = 0;
        var _cAssignObject = $("#cAssignObject").val();
        if (_cAssignObject == 1)
            _RoleID = $("#lsOperateInfo1").attr("vid");
        else
            _RoleID = $("#lsOperateInfo").attr("vid");
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/XT/XT.ashx",
            data: {
                "RoleID": _RoleID,
                "authorityType": _cAssignObject,
                "op": "loadReportRole"
            },
            dataType: "json",
            success: function (rs)
            {
                for (var i = 0; i < rs.length; i++) {

                    $("[vid='" + rs[i]["REPORTID"] + "']:checkbox").prop("checked", "checked");
                }
                
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        form.render("checkbox");
    }

    function loadReport() {
        var _RoleID = 0;
        var _cAssignObject = $("#cAssignObject").val();
        if (_cAssignObject == 1)
            _RoleID = $("#lsOperateInfo1").attr("vid");
        else
            _RoleID = $("#lsOperateInfo").attr("vid");
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/XT/XT.ashx",
            data: {
                "op": "RoleList",
                "RoleID": _RoleID,
               "authorityType": _cAssignObject
            },
            dataType: "json",
            success: function (rs) {
                var fubbstrs = "";
                var strtemp = "";
                var childstr = "";
                var checked = "";
                //加载报表类别
                for (var i = 0; i < rs.length; i++) {
                    if (rs[i]["RoleID"]["Value"] > 0) {
                        checked = "checked";
                    } else { checked = "";}
                    if (rs[i]["I_FUID"]["Value"] == "0") {
                        strtemp = "<div class='reportTitle' vid='"
                        + rs[i]["I_BAOBIAOID"]["Value"] + "'><h2  style='cursor:pointer'> <input type='checkbox'vid='"
                        + rs[i]["I_BAOBIAOID"]["Value"] + "' name='bb' lay-filter='Title' lay-skin='primary'>&nbsp&nbsp<label class='reporth2' style='color:blue;font-size:1.2em; font-weight:bold;'>"
                        + rs[i]["S_BAOBIAOMC"] + "</label></h2> <div><table vid='"
                        + rs[i]["I_BAOBIAOID"]["Value"] + "'class='layui-table' name='ReportList' style='width:100%;' ></table></div></div>";
                        fubbstrs = fubbstrs + strtemp;
                    }
                }
                $("#divList").append(fubbstrs);
                $("table[name='ReportList']").toggle();
                $(".reporth2").click(function () {
                    $(this).parent().parent().find("table").toggle();
                });
                
                //加载具体报表
                $("#divList .reportTitle").each(function () {
                    childstr = "";
                    for (var i = 0; i < rs.length; i++) {
                        if (rs[i]["I_FUID"]["Value"] == $(this).attr("vid")) {

                            strtemp = "<tr class='datarow1' style='cursor:pointer' vid='"
                                + rs[i]["I_BAOBIAOID"]["Value"] + "'><td style='padding-left:20px;font-size:14px'><input type='checkbox' name='bb' parentID="
                                + $(this).attr("vid") + " lay-filter='Detailed' lay-skin='primary' vid='"
                                + rs[i]["I_BAOBIAOID"]["Value"] + "'>&nbsp&nbsp<b>"
                                + rs[i]["S_BAOBIAOMC"] + "</b></td></tr>";
                            childstr = childstr + strtemp;
                        }
                    }
                    $(this).find("table").append(childstr);
                });
                form.render("checkbox");
               
                //绑定报表行点击事件
                $(".datarow1").click(function () {
                    //清除所有背景
                    $(".datarow1").each(function () {
                        $(this).css("background-color", "");
                    });
                    //设置当前选中背景色
                    $(this).css("background-color", "greenyellow");
                   
                });
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
    }

    function loadInfo(id)
    {
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/XT/XT.ashx",
            data: {
                "op": "yhxx_detail",
                "id": id
            },
            dataType: "json",
            success: function (rs) {
                $("#iAccount").val(rs["ACCOUNT"]);
                $("#iAddress").val(rs["ADDRESS"]);
                $("#iBirthday").val(rs["BIRTHDAY"]);
                $("#iBM").val(rs["DEPARTMENT"]);
                $("#iDesc").val(rs["USERDESC"]);
                $("#iEmail").val(rs["EMAIL"]);
                $("#iGender").val(rs["GENDER"]);
                $("#iID_card").val(rs["IDCARD"]);
                $("#iActive").val(rs["ISACTIVE"]);
                $("#iMobilephone").val(rs["MOBILEPHONE"]);
                //$("#iPassword").val(rs["PASSWORD"]);
                $("#iPostcode").val(rs["POSTCODE"]);
                $("#iTelephone").val(rs["TELEPHONE"]);
                $("#iUserName").val(rs["USERNAME"]);
                form.render("select");
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
    }

    function SaveReportRole()
    {
        var arrReportID = new Array();
        var roleid = 0;
        var _cAssignObject = $("#cAssignObject").val();

        $("[name='bb']:checked").each(function () {
            arrReportID.push($(this).attr("vid"));
        });
        arrReportID=arrReportID.join(',');
        if (_cAssignObject == 0) {
            roleid = $("#lsOperateInfo").attr("vid");
        } else if (_cAssignObject == 1) {
            roleid = $("#lsOperateInfo1").attr("vid");
        }
        if (roleid == 0)
        {
            if (_cAssignObject == 0) {
                alert('请选择用户')
            } else if (_cAssignObject == 1) {
                alert('请选择角色')
            }
            return;
        }
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/XT/XT.ashx",
            data: {
                "op": "SaveReportRole",
                "roleid": roleid,
                "arrReportID": arrReportID,
                "authorityType": _cAssignObject
            },
            dataType: "json",
            success: function (rs)
            {
                alert(rs["ReturnMsg"]);
            }
        });
        loadReportRole();
    }

    /*end list*/

    /*logic*/

   
    function clearinfo()
    {
       
    }
    function verify() {
       
        return true;
    }

    /*logic end*/
});

